//
//  BHFunlistHeaderFilterView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/22.
//

import UIKit
protocol BHFunlistHeaderFilterViewDelegate {
    
    // 返回和约玩
    func filterHeaderDidSelectAct(type: Int)

    // 筛选点击
    func filterHeaderDidChose(sender: UIButton)
    
    func funHeaderBegainSearch()
    func funHeaderSearchForKey(key: String)
}

class BHFunlistHeaderFilterView: UIView {

    var delegate: BHFunlistHeaderFilterViewDelegate?
    
    
    @IBOutlet weak var space_to_top: NSLayoutConstraint!
    
    @IBOutlet weak var filterTextFiled: UIView!
    
    @IBOutlet weak var searchTextFiled: UITextField!
    @IBOutlet var filterBtnArray: [UIButton]!
    
    fileprivate var remarkBtn: UIButton?
    
    static func loadFromXib() -> BHFunlistHeaderFilterView {
        let viewArray = Bundle.main.loadNibNamed("BHFunlistHeaderFilterView", owner: nil, options: nil)
        let view: BHFunlistHeaderFilterView = viewArray![0] as! BHFunlistHeaderFilterView
        return view
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.filterTextFiled.addLayerCorner(16, 0, .white)
        self.searchTextFiled.delegate = self
        self.space_to_top.constant = UIScreen.stateBarHeight
        
        for btn in self.filterBtnArray {
            btn.setTitleColor(UIColor.white.withAlphaComponent(0.7), for: .normal)
            btn.setTitleColor(UIColor.white.withAlphaComponent(1), for: .selected)
        }
    }
    
    
    @IBAction func headerBackAct(_ sender: UIButton) {
        self.delegate?.filterHeaderDidSelectAct(type: 0)
    }
    
    
    
    @IBAction func goHaveFunAct(_ sender: UIButton) {
        self.delegate?.filterHeaderDidSelectAct(type: 1)
    }
    
    
    @IBAction func filterDidAct(_ sender: UIButton) {
        if sender.isSelected == true {
            return
        }
        sender.isSelected = true
        
        self.remarkBtn?.isSelected = false
        
        self.delegate?.filterHeaderDidChose(sender: sender)
        
        self.remarkBtn = sender
    }
    
    func changeIndexBtnText(index: Int,text: String)  {
        if index < self.filterBtnArray.count {
            let btn = self.filterBtnArray[index]
            btn.setTitle(text, for: .normal)
            btn.setTitle(text, for: .selected)
        }
    }
    func resetAllState()  {
        for btn in self.filterBtnArray {
            btn.isSelected = false
        }
        self.remarkBtn = nil
    }
    
    static var height: CGFloat {
        get {
            return CGFloat(162)+UIScreen.stateBarHeight
        }
    }

}
extension BHFunlistHeaderFilterView: UITextFieldDelegate {
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        self.delegate?.funHeaderBegainSearch()
        return true
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
        self.delegate?.funHeaderSearchForKey(key: textField.text ?? "")
    }
}
